package com.example.bicycle.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.example.bicycle.mqtt.impl.MqttMessageServiceImpl.clientsStatusMap;

/**
 * MQTT测试控制器
 */
@Slf4j
@SaCheckLogin
@RestController
@RequestMapping("/mqtt/")
public class MqttTestController {

    /**
     * 获取所有客户端状态
     */
    @GetMapping("clients-status")
    public SaResult getClientsStatus() {
        log.info("当前客户端状态: {}", clientsStatusMap);
        return SaResult.data(clientsStatusMap);
    }

    /**
     * 获取在线客户端数量
     */
    @GetMapping("online-count")
    public SaResult getOnlineCount() {
        long onlineCount = clientsStatusMap.values().stream()
                .mapToLong(status -> status ? 1 : 0)
                .sum();
        return SaResult.data(Map.of(
                "onlineCount", onlineCount,
                "totalCount", clientsStatusMap.size()
        ));
    }
} 